package com.atguigu.service.impl;

import com.atguigu.dao.DictDao;
import com.atguigu.entity.Dict;
import com.atguigu.service.DictService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class DictServiceImpl implements DictService {

    @Autowired
    private DictDao dictDao;
    @Override
    public List<Map<String, Object>> findZNodes(Long id) {
            List<Dict> dictList = dictDao.findDictByParentId(id);
        List<Map <String,Object>> znodes = new ArrayList<>();
        for (Dict dict : dictList) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",dict.getId());
            map.put("name",dict.getName());
            //获取dict是否有子级,并且查看子级的个数是否大于0
            map.put("isParent",dictDao.findDictByParentId(dict.getId()).size() > 0);
            //将map集合添加到znodes中去
            znodes.add(map);
        }
        return znodes;
    }

    @Override
    public List<Dict> findDictByParentCode(String ParentCode) {
        //根据code找到id
        Dict dict = dictDao.getDictByCode(ParentCode);
        //根据id查找查找子级
        return dictDao.findDictByParentId(dict.getId());
    }

    @Override
    public List<Dict> findDictByParentId(Long areaId) {
        return dictDao.findDictByParentId(areaId);
    }

    //修改代码测试用
}
